﻿using Project_veilingsite.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebMatrix.WebData;

namespace Project_veilingsite.Controllers
{
    public class HomeController : Controller
    {
        private dtb_Veilingssite _db = new dtb_Veilingssite();

        [HttpGet]
        public ActionResult Index()
        {
            try
            {
                var model = _db.Veilingen.ToList().Where( r => r.vervaldag > DateTime.Now ).OrderByDescending(r => r.datum);

                return View(model);
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/LoggingTracing/tracingLogging.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException: " + e.StackTrace);
                }
            }
            return null;
        }

        [HttpPost]
        public ActionResult Index(String searchstring)
        {
            try
            {
                var model =
                    _db.Veilingen
                    .OrderByDescending(r => r.datum)
                    .Where(r => searchstring == null || r.titel.StartsWith(searchstring)).Where(r => r.vervaldag > DateTime.Now)
                    ;
                //alle gegevens worden opgehaald / de gegevens die beginnen met seacrhstring worden opgehaald

                //de gegevens worden in tempData gezet zodat deze kunnen worden doorgegeven aan de search controller
                TempData["SearchModel"] = model;

                return RedirectToAction("Index", "Veiling");
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/LoggingTracing/tracingLogging.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException: " + e.StackTrace);
                }
            }
            return null;
        }

        //Multilanguage
        public ActionResult ChangeCulture(string lang)
        {
            try
            {
                var langCookie = new HttpCookie("lang", lang) { HttpOnly = true };
                Response.AppendCookie(langCookie);
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/LoggingTracing/tracingLogging.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException: " + e.StackTrace);
                }
            }
            return RedirectToAction("Index", "Home", new { culture = lang });
        }

        [HttpPost]
        public ActionResult ShowAuction(VeilingModel model)
        {
            return View(model);
        }

        public ActionResult Account()
        {
            ViewBag.Message = "Your account";
            return View();
        }

        [Authorize]
        public ActionResult OwnAuctions()
        {
            try
            {
                int userID = WebSecurity.CurrentUserId; //GebruikersID opvragen
                var model =
                    _db.Veilingen
                    .OrderByDescending(r => r.datum)
                    .Where(r => r.gebruikersid == userID);

                return View(model);
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/LoggingTracing/tracingLogging.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException: " + e.StackTrace);
                }
            }
            return null;
        }

        [Authorize]
        public ActionResult WinningAuctions()
        {
            try
            {
                int userID = WebSecurity.CurrentUserId; //GebruikersID opvragen
                var model = _db.Veilingen.Where(r => r.laatsteBieder == userID).OrderByDescending(r => r.vervaldag);
                ViewData["won"] = "true"; //Om een biedknop te krijgen of niet - false = biedknop

                return View(model);
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/LoggingTracing/tracingLogging.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException: " + e.StackTrace);
                }
            }
            return null;
        }

        [Authorize]
        public ActionResult WonAuctions()
        {
            try
            {
                int userID = WebSecurity.CurrentUserId; //GebruikersID opvragen
                var model = _db.Veilingen.Where(r => r.laatsteBieder == userID).Where(r => r.vervaldag < DateTime.Now).OrderByDescending(r => r.vervaldag);

                return View(model);
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/LoggingTracing/tracingLogging.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException: " + e.StackTrace);
                }
            }
            return null;
        }
    }
}